package daos

import (
	"gitee.com/ai-love-save/ai-love-save-back/app/entity"
	"gitee.com/ai-love-save/ai-love-save-back/app/utils"
	"github.com/google/wire"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

// UserSet User DI
var UserSet = wire.NewSet(wire.Struct(new(User), "*"))

// User 用户相关
type User struct {
	DB     *gorm.DB
	Logger *zap.Logger
}

type UserGetParams struct {
	Phone string
}

func (u *User) Get(params interface{}) (error, interface{}) {
	resultParams, ok := params.(UserGetParams)
	if !ok {
		u.Logger.Error("参数传递错误, 应为 UserGetParams 类型")
		return utils.ErrTypeMismatch, nil
	}

	db := entity.GetUser(u.DB)

	if v := resultParams.Phone; v != "" {
		u.Logger.Info("使用 phone 查找", zap.String("uid", v))
		db = db.Where(map[string]interface{}{"name": v})
	}

	var user entity.User
	if err := db.First(&user).Error; err != nil {
		return err, nil
	}

	return nil, user
}
